using UnityEngine;
using Yunchang;

namespace AutoChess
{
    public enum GridType
    {
        //和服务器对应
        FightGrid = 0,
        PreChooseGrid = 1,
        ShopGrid = 2 ,

        //客户端
        EnemyGrid = 3,
        CenterGrid = 4,
    }
    class ACGrid:GameActor
    {
        
        //TODO 把 这一堆东西去掉
        public ACHero heroUnit;
        private int Row, Column;
        private int localIndex;
        private GridType gridType;
        private Vector3 size;
        private Vector3 position;
        private Vector3 boxOffset;
        private bool canEnter = true;

        private bool isSelect = false;
        public int RowIndex
        {
            get { return Row; }
        }
        public int ColumnIndex
        {
            get { return Column; }
        }
        public bool IsSelect
        {
            get {  return isSelect; }
            set { isSelect = value; }
        }
        public GridType GridType
        {
            get { return gridType; }
            set { gridType = value; }
        }
        public int LocalIndex
        {
            get { return localIndex; }
            set { localIndex = value; }
        }
        public int ServerIndex
        {
            get { return (int)gridType *100 + localIndex + 1; }
        }
        public int EnemyServerIndex
        {
            get { return (int)GridType.FightGrid*100 + localIndex + 1; }
        }
        
        
        public Vector3 Position
        {
            get { return position; }
            private set { position = value;}
        }
        public Vector3 Size
        {
            get { return size; }
            set { size = value; }
        }
        public Vector3 BoxOffset
        {
            get { return boxOffset; }
            set { boxOffset = value; }
        }
        public bool CanEnter
        {
            get { return canEnter; }
        }

        public override ActorType GetActorType()
        {
            return ActorType.ChessGrid;
        }

        public bool CreateACGrid(ACGridData gridData)
        {
            this.localIndex = gridData.localIndex;
            this.Row = gridData.rowIndex;
            this.Column = gridData.columeIndex;
            this.gridType = gridData.gridType;
            this.position = gridData.gridPos;
            this.size = gridData.boxColliderSize;
            this.boxOffset = gridData.boxColliderOffset;

            BUnityUtil.SetLayer(ActorGo,gridData.gridType == GridType.ShopGrid?
                GameLayer.UILayer : GameLayer.ActorLayer);
           
            return true;
        }
        
        public override void OnEnterMap()
        {
            Event.AddEventListener(ActorEventType.OnActorLeaveGrid,OnActorLeaveGrid,this);
            Event.AddEventListener<ACHero>(ActorEventType.OnActorEnterGrid,OnActorEnterGrid,this);
            Event.AddEventListener<Transform,bool>(ActorEventType.OnGridChangeParentRoot,OnGridChangeParentRoot,this);
        }

        #region EventCallback

        /// <summary>
        /// 变更格子的父节点
        /// </summary>
        private void OnGridChangeParentRoot(Transform newParent,bool upDown)
        {
            var trans = ActorTrans.transform;
            ActorTrans.transform.SetParent(newParent);
            if (upDown)
            {
                trans.localPosition = Vector3.zero;
                trans.localScale = new Vector3(360, 360, 360);//TODO 暂时先写死
                Position = trans.position;
            }
            else
            {
                trans.position = new Vector3(1000,1000,1000);
                Position = new Vector3(1000, 1000, 1000);
            }
        }
        
        private void OnActorLeaveGrid()
        {
            heroUnit = null;
        }

        private void OnActorEnterGrid(ACHero enterHero)
        {
            heroUnit = enterHero;
        }

        #endregion
    }
}